import { post } from '@/http/axios'
import { IsArray } from '@/util'
import { handleData, normalMap } from '@/util/data'

const config = [
  [
    'id',
    'group_name',
    'position_name',
    'group_name',
    'position_name',
    'id',
    'childrens',
    'positions',
    'is_out',
    'is_group',
    'remark'
  ],
  [
    'id',
    'label',
    'label',
    'gname',
    'pname',
    'value',
    'children',
    'children',
    'out',
    'group',
    'remark'
  ]
]
// token
export const getGroupData = async (...args) => {
  const { code, data, cancelMessage } = await post('/searchGroupList', ...args)
  if (data) {
    const tree = handleData(data, config)
    return { code, data: tree, cancelMessage }
  }
  return { code, cancelMessage }
}

// duty_man	负责人
// group_code	代码
// group_name	组名
// group_spell	组名拼音
// parent_id	父id
// remark	部门描述
// org_id 厂商
export const addGroupData = (...args) => post('/addGroup', ...args)

// group_id
// parent_id	父id
// group_name	组名
// remark	部门描述
export const editGroupData = (...args) => post('/editGroup', ...args)

// group_id
export const delGroupData = (...args) => post('/removeGroup', ...args)

// token
// group_id	部门id
export const getGroupInfo = (...args) => post('/searchGroupInfo', ...args)

// 部门下拉框
const groupConfig = [
  ['id', 'group_name', 'id', 'childrens', 'is_out', 'remark', 'org_id'],
  ['id', 'label', 'value', 'children', 'out', 'remark', 'org_id']
]
export const getGroupOption = async (...args) => {
  const { code, data, cancelMessage } = await post(
    '/searchSelectGroupList',
    ...args
  )
  if (data) {
    const tree = handleData(data, groupConfig)
    const map = normalMap(tree)
    return { code, data: tree, map, cancelMessage }
  }
  return { code, cancelMessage }
}
// --------------------------------------------------岗位--------------------------------------------------

// group_id	部门id
// position_name	岗位名称
// remark	部门描述
export const addPositionData = (...args) => post('/addPosition', ...args)

// position_id	部岗位id
// group_id	部门id
// position_name	岗位名称
// remark	部门描述
export const editPositionData = (...args) => post('/editPosition', ...args)

//position_id	部岗位id
export const delPositionData = (...args) => post('/removePosition', ...args)

// token
// position_id	岗位id
export const getPositionInfo = (...args) => post('/searchPositionInfo', ...args)

// token
// position_id	部岗位id
export const getPositionRoleInfo = (...args) =>
  post('/searchPositionRoleList', ...args)

// token
// position_id	部岗位id
export const getPositionRoleData = (...args) =>
  post('/searchAddPositionRoleList', ...args)

// addPositionRole
// position_id	部岗位id
// roleIds	角色ids
export const addPositionRoleData = (...args) =>
  post('/addPositionRole', ...args)

// position_id	部岗位id
// roleIds	角色ids
export const delPositionRoleData = (...args) =>
  post('/removePositionRole', ...args)
//岗位下拉框
const positionConfig = [
  [
    'id',
    'group_name',
    'position_name',
    'id',
    'childrens',
    'positions',
    'is_out',
    'is_group',
    'remark'
  ],
  [
    'id',
    'label',
    'label',
    'value',
    'children',
    'children',
    'out',
    'disabled',
    'remark'
  ]
]
const toPosition = (data, pl) => {
  const hd = (arr, item) => {
    const { disabled, label, children, value, out } = item
    const ol = out ? '(外)' : ''
    const nl = pl ? pl + '/' + label + ol : label + ol
    const obj = { label: nl, value }
    if (disabled) {
      const na = toPosition(children, nl)
      na.length && arr.push(...na)
    } else {
      arr.push(obj)
    }
    return arr
  }
  return IsArray(data) ? data.reduce(hd, []) : []
}

export const getPositionOption = async (...args) => {
  const { code, data, cancelMessage } = await post(
    '/searchSelectGroupList',
    ...args
  )
  if (data) {
    const tree = toPosition(handleData(data, positionConfig))
    const map = normalMap(tree)

    return { code, data: tree, map, cancelMessage }
  }
  return { code, cancelMessage }
}
